package com.he.architecture.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.he.architecture.Enums.CustomerOrderTypeEnums;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;


@Entity
@Getter
@Setter
//商家标志
@DiscriminatorValue("1")
public class Business extends User{
    @Column
    private String businessName;
    @Column
    private String businessImg;

    //商家和门店的一对多关系
    // orphanRemoval = true
    // 用于自动删除没有父实体引用的“孤儿”实体
    @OneToMany(mappedBy = "business",
            fetch = FetchType.LAZY,
            orphanRemoval = true,
            cascade = CascadeType.ALL)
    @JsonIgnoreProperties(value = {"business"})
    private Set<Shop> shops;


    //商家和商品的一对多
    @OneToMany(mappedBy = "business",
            fetch = FetchType.LAZY,
            orphanRemoval = true,
            cascade = CascadeType.ALL)
    @JsonIgnoreProperties(value = {"business"})
    private Set<ShopItem> shopItems;

    /**
     * 商家获取门店的id列表
     */
    public List<Long> getShopIds(){
        List<Long> ids=new ArrayList<>();
        for (Shop shop : shops) {
            ids.add(shop.getId());
        }
        return ids;
    }
    /**
     * 商家创建门店
     */
    public Set<Shop> addShop(Shop shop){
        //添加到门店列表中
        shops.add(shop);
        //更新
        setShops(shops);
        return shops;
    }
    /**
     * 新建商品
     */
    public Set<ShopItem> createShopItem(ShopItem shopItem){
        //新商品添加
        shopItems.add(shopItem);
        setShopItems(shopItems);
        return shopItems;
    }
    /**
     * 确认订单
     */
    public CustomerOrder confirmOrder(CustomerOrder customerOrder){
        //订单状态修改
        customerOrder.setOrderType(CustomerOrderTypeEnums.CONFIRM.getCode());
        return customerOrder;
    }
}
